#include "GameBoardGenerator.h"

namespace TUMAR { namespace CountryRoads {

	GameBoardGenerator::GameBoardGenerator(GameBoard* board) : board(board)
	{
		std::srand(time(NULL));
	}
	
	GameBoardGenerator::~GameBoardGenerator(void)
	{
	}
 
	void GameBoardGenerator::GenerateStartAndEnd(void)
	{
		this->board->StartTile = this->GenerateStraightRoadAtBorder(NULL);
		this->board->Character->initPosition(this->board->StartTile->Position[0],this->board->StartTile->Position[1],this->board->StartTile->Orientation);
		this->board->EndTile = this->GenerateStraightRoadAtBorder(this->board->StartTile);
	}

	Tile* GameBoardGenerator::GenerateStraightRoadAtBorder(Tile* other)
	{
		int x, y, orientation;
		Tile* tile;
		bool done = false;

		while(!done)
		{
			if(std::rand() % 2 == 0)
			{
				x = (std::rand() % 2) * (this->board->GetWidth() - 1);
				y = 1 + std::rand() % (this->board->GetHeight() - 2);
				orientation = x == 0 ? 1 : 3;
			}
			else
			{
				x = 1 + std::rand() % (this->board->GetWidth() - 2);
				y = (std::rand() % 2) * (this->board->GetHeight() - 1);
				orientation = y == 0 ? 2 : 0;
			}

			// TODO: check that distance is > 5
			if(other != NULL && (other->Position[0] == x || other->Position[1] == y))
				continue;

			tile = this->board->GetTile(x, y);

			if(tile->GetType() == Empty)
			{
				tile->SetType(RoadStraight);
				tile->Orientation = orientation;
				done = true;
			}
		}

		return tile;
	}

	void GameBoardGenerator::GenerateBorder(void)
	{
		int x, y;
		Tile* tile;

		y = 0;
		for(x = 0; x < this->board->GetWidth(); x++)
		{
			tile = this->board->GetTile(x, y);
			if(tile->GetType() == Empty)
			{
				tile->SetType(Border);
			}
		}

		y = this->board->GetHeight() - 1;
		for(x = 0; x < this->board->GetWidth(); x++)
		{
			tile = this->board->GetTile(x, y);
			if(tile->GetType() == Empty)
			{
				tile->SetType(Border);
			}
		}

		x = 0;
		for(y = 0; y < this->board->GetHeight(); y++)
		{
			tile = this->board->GetTile(x, y);
			if(tile->GetType() == Empty)
			{
				tile->SetType(Border);
			}
		}

		x = this->board->GetWidth() - 1;
		for(y = 0; y < this->board->GetHeight(); y++)
		{
			tile = this->board->GetTile(x, y);
			if(tile->GetType() == Empty)
			{
				tile->SetType(Border);
			}
		}
	}

	void GameBoardGenerator::GenerateObstacles(void)
	{
		int obstacles = 0;

		// extremely simple random obstacle placement
		while(obstacles < 4)
		{
			// don't put obstacles in the border tiles
			int x = 1 + std::rand() % (this->board->GetWidth() - 2);
			int y = 1 + std::rand() % (this->board->GetHeight() - 2);
			
			TileType type = (std::rand() % 2) == 0 ? ObstacleStone : ObstacleHayBail;
			int orientation = std::rand() % 4;

			Tile* tile = this->board->GetTile(x, y);
			// TODO: check that it doesn't block start or end
			if(tile->GetType() == Empty)
			{
				tile->SetType(type);
				tile->Orientation = orientation;
				obstacles++;
			}
		}
	}
}}